<?php

namespace app\admin\controller;

use think\Db;
use think\response\Json;

Class Article extends Base
{
    public function edit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_article")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data["type"])||$data["type"]<1){
                return ['status' => 'n', 'info' => '请选择类型'];
            }
            $sqldata = array();
            $sqldata["title"]=$data["title"];
            $sqldata["content"]=$data["content"];
            $sqldata["introduce"]=$data["introduce"];
            $sqldata["type"]=$data["type"];
            $sqldata["time"]=time();
            if (empty($id)){
                $res = Db::table("tb_article")->insert($sqldata);
            }else{
                $res = Db::table("tb_article")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        $types = Db::table("tb_article_type")->where("status",1)->select();
        return view('', ['row' => $row,'types' => $types]);
    }

    public function list(){
        $type = $this->request->get('type');
        $title = $this->request->get('title');
        $where = array();
        if (!empty($type)&&$type>0){
            $where[] = ['type', 'eq', $type];
        }
        if ($title){
            $where[] = ['title', 'like', '%'.$title.'%'];
        }
        $list = Db::table("view_article")->where($where)->order("type asc ,status desc")->paginate(10, false, ['query' => $this->request->param()]);

        $types = Db::table("tb_article_type")->where("status",1)->select();
        return view('', ['list' => $list,'types' => $types]);
    }

    public function status(){
        $data = $this->request->post();
        $status = $data['status'] == 0 ? 1 : 0;
        $res = Db::table("tb_article")->where('id', $data['id'])->setField('status', $status);
        if ($res) {
            return ['status' => 'y', 'info' => '操作成功'];
        } else {
            return ['status' => 'n', 'info' => '操作失败'];
        }
    }

    public function typeEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_article_type")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sqldata = array();
            $sqldata["title"]=$data["title"];
            $sqldata["time"]=time();
            if (empty($id)){
                $res = Db::table("tb_article_type")->insert($sqldata);
            }else{
                $res = Db::table("tb_article_type")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        return view('', ['row' => $row]);
    }

    public function typeList(){
        $list = Db::table("tb_article_type")->order("status desc")->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function typeStatus(){
        $data = $this->request->post();
        $status = $data['status'] == 0 ? 1 : 0;
        $res = Db::table("tb_article_type")->where('id', $data['id'])->setField('status', $status);
        if ($res) {
            return ['status' => 'y', 'info' => '操作成功'];
        } else {
            return ['status' => 'n', 'info' => '操作失败'];
        }
    }



}